package com.shycloud.mido.member.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.SmCoursewareGroup;
import com.shycloud.mido.member.api.entity.SmCoursewareGroupFolder;
import com.shycloud.mido.member.api.vo.SmCoursewareGroupVO;
import com.shycloud.mido.member.mapper.SmCoursewareGroupFolderMapper;
import com.shycloud.mido.member.mapper.SmCoursewareGroupMapper;
import com.shycloud.mido.member.service.SmCoursewareGroupService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class SmCoursewareGroupServiceImpl extends ServiceImpl<SmCoursewareGroupMapper, SmCoursewareGroup> implements SmCoursewareGroupService {

	/** 常量名：默认分组 */
	public static final String DEFAULT_GROUP = "默认分组";

	/** 课件分组模块 mapper */
	private SmCoursewareGroupMapper smCoursewareGroupMapper;
	/** 课件分组-文件夹关系表 */
	private SmCoursewareGroupFolderMapper smCoursewareGroupFolderMapper;

	/**
	 * 获取分组列表
	 *
	 * @return java.util.List<com.shycloud.mido.member.api.vo.SmCoursewareGroupVO>
	 * @author nianhua.jiang
	 * @date 2020/10/10 15:00
	 **/
	@Override
	public List<SmCoursewareGroupVO> getGroupList() {

		String memberId = SecurityUtils.getMember().getId();

		//判断此用户是否有分组名为【默认分组】的分组信息
		SmCoursewareGroup smCoursewareGroup = smCoursewareGroupMapper.selectOne(new QueryWrapper<SmCoursewareGroup>()
				.eq(SmCoursewareGroup.COL_MEMBER_ID, memberId)
				.eq(SmCoursewareGroup.COL_NAME, DEFAULT_GROUP));

		//如果没有则新建
		if (smCoursewareGroup == null) {
			SmCoursewareGroup smCoursewareGroupNew = new SmCoursewareGroup();
			//用户ID
			smCoursewareGroupNew.setMemberId(memberId);
			//分组名为默认分组
			smCoursewareGroupNew.setName(DEFAULT_GROUP);
			//排序
			smCoursewareGroupNew.setSort(1);
			smCoursewareGroupMapper.insert(smCoursewareGroupNew);
		}

		return smCoursewareGroupMapper.getGroupList(memberId);
	}

	/**
	 * 删除分组
	 *
	 * @param id
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/10/10 17:24
	 **/
	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public Boolean delGroup(Integer id) {

		String memberId = SecurityUtils.getMember().getId();

		//获取【默认分组】的主键 ID
		SmCoursewareGroup smCoursewareGroup = smCoursewareGroupMapper.selectOne(new QueryWrapper<SmCoursewareGroup>()
				.eq(SmCoursewareGroup.COL_MEMBER_ID, memberId)
				.eq(SmCoursewareGroup.COL_NAME, DEFAULT_GROUP));

		if (smCoursewareGroup == null) {
			throw new BusinessException("默认分组信息获取失败，请联系管理员");
		}

		//删除分组
		smCoursewareGroupMapper.deleteById(id);
		//将分组下的文件夹转移到【默认分组】下
		List<SmCoursewareGroupFolder> smCoursewareGroupFolders = smCoursewareGroupFolderMapper.selectList(new QueryWrapper<SmCoursewareGroupFolder>().eq(SmCoursewareGroupFolder.COL_GROUP_ID, id));

		for (SmCoursewareGroupFolder smCoursewareGroupFolder : smCoursewareGroupFolders) {
			smCoursewareGroupFolder.setGroupId(smCoursewareGroup.getId());
			smCoursewareGroupFolderMapper.updateById(smCoursewareGroupFolder);
		}

		return true;
	}
}

